﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace HobSoft.SoccerBet2014.Domain.Implementation
{
    using Core;
    using Models.Core;
    using SoccerBet2014.Common.DB.Queryable;

    public abstract class ReadonlySqlEntRepository<T> : IReadonlyRepository<T>
        where T : class, IModel
    {
        public ReadonlySqlEntRepository(DbContext dbContext)
        {
            if (dbContext == null)
                throw new ArgumentNullException("dbContext", "need dbContext.");

            DbContext = dbContext;
            DbSet = dbContext.Set<T>();
        }

        protected readonly DbContext DbContext;
        protected readonly IDbSet<T> DbSet;

        public virtual T Get(params object[] id)
        {
            if (id.Length == 0) return null;
            return DbSet.Find(id);
        }

        public virtual IList<T> GetMany(Expression<Func<T, bool>> predicate)
        {
            if (predicate == null)
                throw new ArgumentNullException("predicate", "need predicate.");

            return DbSet.Where(predicate).ToList();
        }

        public virtual IList<T> GetMany(Expression<Func<T, bool>> predicate, params SoccerBet2014.Common.Queryable.ISortExpression<T>[] sortExpression)
        {
            if (predicate == null)
                throw new ArgumentNullException("predicate", "need predicate.");
            if (sortExpression == null)
                throw new ArgumentNullException("sortExpression", "need sortExpression.");

            return DbSet.Where(predicate).Sort(sortExpression).ToList();
        }
    }
}
